const mysql=require('mysql')

const db=mysql.createPool({
	host: '127.0.0.1',
	user: 'root',
	password: 'root123',
	database: 'node_demo'
})
// 测试连接
db.query('SELECT 1', (err,results)=>{
	if(err) return console.log(err.message)
	console.log(results)
})

// 查询数据
db.query('select * from users',(err,results)=>{
	if(err) return console.log(err.message)
	console.log(results)
})

// 插入
const user = {username:'lisi', password: '123'}
const update_sql='insert into users set ?'
db.query(update_sql, user, (err,results)=>{
	if(err) return console.log(err.message)
	if(results.affectedRows === 1){
		console.log('插入成功')
	}
}
)

// 更新
const user = {id: 3, username:'lisi', password: '1234'}
const sql = 'update users set ? where id=?'
db.query(sql,[user,user.id],(err,results)=>{
	if(err) return console.log(err.message)
	if(results.affectedRows === 1) {
		console.log('更新成功')
	}
})
// 删除
const sql='delete from users where id=?'
db.query(sql,3,(err, results)=>{
	if(err) return console.log(err.message)
	if(results.affectedRows === 1){
		console.log('删除成功！')
	}
})